﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace LCH
{
    public enum UVMark
    {
        UV0 = 0,
        UV1 = 1,
        UV2 = 2,
    }
    partial class LchMeshPainter
    {
        int editorLayer = 30;
        
        UVMark uvMark = UVMark.UV0;
        int panelType = 0;
        int vertexBrushType = 0;
        int meshPanelType = 0;
        int drawType;
        bool enable = true;
        bool isMouseOnMesh = false;
        bool isDragging = false;
        bool isTextureChannelDirty = false;
        bool isTextureFlowMaplDirty = false;
        bool isFlowMapDirty = false;
        bool isVertexDirty = false;
        bool isTextureChannelModify = false;
        bool isVertexModify = false;
        float dirtyTime = 0f;

        // 控制图管理器
        ControlTextureManager controlTextureManager;
        
        // 为了向后兼容，保留这些属性的访问器
        List<string> detectedCtrlTextureNames 
        { 
            get { return controlTextureManager?.DetectedControlTextureNames ?? new List<string>(); } 
        }
        int controlTextureCount 
        { 
            get { return controlTextureManager?.ControlTextureCount ?? 0; } 
        }
        int maxTextureCount 
        { 
            get { return controlTextureManager?.MaxTextureCount ?? ControlTextureConstants.DEFAULT_TEXTURE_COUNT; } 
        }
        bool hasFlowMap 
        { 
            get { return controlTextureManager?.HasFlowMap ?? false; } 
        }
        bool flowMapEditing = false;

        int editingChannel {
            get {
                return _editingChannel;
            }
            set
            {
                if(_editingChannel != value)
                    _editingChannel = value;
            } 
               
        }
        int _editingChannel = -1;

        bool textureNeedPaint = false;

 

        bool vertexNeedPaint = false;
        // 动态控制图数组，根据检测到的控制图数量分配
        Texture2D[] ctrlText;
        RenderTexture[] editorCtrl1RT;
        
        // 初始化控制图管理器
        void InitializeControlTextureManager()
        {
            if (controlTextureManager == null)
            {
                controlTextureManager = new ControlTextureManager(setting);
            }
        }
        Texture2D flowMapTex;
        RenderTexture editorFlowMapRT;
        RenderTexture testRt;
        RenderTextureTypeUndoRedo texturePaninnerUndoRedo = new RenderTextureTypeUndoRedo();
        VertexUndoRedo vertexUndoRedo = new VertexUndoRedo();
        //Terrain
        TerrainTools terrainTools = new TerrainTools();

        LchUVAreaPerviewTexture uvAreaPerviewTexture = new LchUVAreaPerviewTexture();
        PreviewHelper previewHelper = new PreviewHelper();

        RenderTexture brushTexture;
        MeshFilter selectObject;

        Texture2DArray selectTexture2dArray;
        int[] t2dAryIndex;
        GUIContent[] t2dAryIcons;
        Texture2D[] tempImage;
        MeshRenderer selectMeshObjectRender;
        Material selectObjectMaterial;
        Material lastSelectObjectMaterial;
        MeshFilter lastSelectObject;
        MeshCollider selectMeshCollider;
        MeshRenderer selectMeshRender;
        GameObject brush;
        Material brushMat2;
        Material blurMat;
        Material blendMat;
        Material brushing;
        Material brushing_flowmap;
        Material perviewMat;
        Color vertexBrushColor = Color.red;

        Color[] baseColors = null;
        Mesh selectObjectMesh;
        Mesh lastSelectObjectMesh;

        // 动态检测Shader中的控制图（使用ControlTextureManager）
        void DetectControlTextures(Shader shader)
        {
            InitializeControlTextureManager();
            controlTextureManager.DetectControlTextures(shader);
        }

        //释放图标
        void ReleaseIcons()
        {
            if (null != t2dAryIcons)
            {
                for (int i = 0; i < t2dAryIcons.Length; i++)
                {
                    if(null != t2dAryIcons[i] && null != t2dAryIcons[i].image)
                        GameObject.DestroyImmediate(t2dAryIcons[i].image, true);
                    if (tempImage != null && i < tempImage.Length)
                        tempImage[i] = null;
                }
            }
            t2dAryIcons = null;
            if (tempImage != null)
            {
                for (int i = 0; i < tempImage.Length; i++)
                {
                    tempImage[i] = null;
                }
            }
            tempImage = null;
            t2dAryIndex = null;
            selectTexture2dArray = null;
        }
        
        // 释放控制图资源
        void ReleaseControlTextures()
        {
            ControlTextureOperations.ReleaseControlTextures(ref ctrlText, ref editorCtrl1RT);
        }
        Material tex3DCopyMat;
        Material tex2DCopyNoAlphaMat;
        void CheckIconArray()
        {
            InitializeControlTextureManager();
            
            // 如果材质存在，重新检测控制图
            if (selectObjectMaterial != null && selectObjectMaterial.shader != null)
            {
                DetectControlTextures(selectObjectMaterial.shader);
            }
            
            // 如果数组不存在或大小不匹配，重新创建
            if (null == t2dAryIcons || t2dAryIcons.Length != maxTextureCount)
            {
                // 释放旧的图标
                ReleaseIcons();
                
                // 创建新的数组
                t2dAryIcons = new GUIContent[maxTextureCount];
                tempImage = new Texture2D[maxTextureCount];
                t2dAryIndex = new int[maxTextureCount];
                
                for (int i = 0; i < maxTextureCount; i++)
                {
                    t2dAryIcons[i] = new GUIContent();
                    t2dAryIndex[i] = i;
                    tempImage[i] = null;
                }
            }
            
            // 确保控制图数组大小正确（使用辅助方法）
            ControlTextureOperations.EnsureControlTextureArrays(controlTextureCount, ref ctrlText, ref editorCtrl1RT);
            
            GameObject.DestroyImmediate(tex2DCopyNoAlphaMat);
            GameObject.DestroyImmediate(tex3DCopyMat);
        }
         
        void CreateCommonIcons()
        {
            CheckIconArray();
            if (null == tex2DCopyNoAlphaMat)
                tex2DCopyNoAlphaMat = new Material(Shader.Find("Hidden/CopyTexureNoAlpha"));
            if (null != selectObjectMaterial && t2dAryIcons != null)
            {
                int count = Mathf.Min(maxTextureCount, t2dAryIcons.Length);
                for (int i = 0; i < count; i++)
                {
                    var ct = t2dAryIcons[i];
                    string propertyName = setting.prefix + i;
                    if (selectObjectMaterial.HasProperty(propertyName))
                    {
                         Texture2D img = selectObjectMaterial.GetTexture(propertyName) as Texture2D;
                        if (img != tempImage[i])
                        {
                            tempImage[i] = img;
                            RenderTexture rt;
                            if (null == ct.image)
                                rt = new RenderTexture(ControlTextureConstants.ICON_SIZE, ControlTextureConstants.ICON_SIZE, 0);
                            else
                                rt = (RenderTexture )ct.image;
                            Graphics.Blit(img, rt, tex2DCopyNoAlphaMat);
                            ct.image = rt;
                            ct.text = i.ToString();
                        }
                    }
                    else
                    {
                        ct.text = i.ToString();
                    }
                    t2dAryIcons[i] = ct;
                }
            }
        }
        void CreateT2dAryIcons()
        {
            CheckIconArray();
            if(null== tex3DCopyMat)
                tex3DCopyMat = new Material(Shader.Find("Hidden/Tex2dArayToTex"));
            if (t2dAryIcons != null && t2dAryIndex != null)
            {
                int count = Mathf.Min(maxTextureCount, t2dAryIcons.Length, t2dAryIndex.Length);
                for (int i = 0; i < count; i++)
                {
                    var ct = t2dAryIcons[i];
                    if (null == selectTexture2dArray)
                    {
                        ct.image = null;
                        ct.text = i.ToString();
                    }
                    else
                    {
                        tex3DCopyMat.SetInt("_Index", t2dAryIndex[i]);
                        RenderTexture rt;
                        if (null == ct.image)
                            rt = new RenderTexture(ControlTextureConstants.ICON_SIZE, ControlTextureConstants.ICON_SIZE, 0);
                        else
                            rt = (RenderTexture)ct.image;
                        Graphics.Blit(selectTexture2dArray, rt, tex3DCopyMat);
                        ct.image = rt;
                        ct.text = "";
                    }
                    t2dAryIcons[i] = ct;
                }
            }
        }

         
    }
}

